package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbUserMapper;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.pojo.TbUserExample;
import com.dongdongshop.service.UserService;
import com.dongdongshop.shiro.ShiroUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@DubboService
public class UserServiceImpl implements UserService {

    @Autowired
    private TbUserMapper tbUserMapper;

    @Override
    public void add(TbUser tbUser) {
        //撒盐
        String salt = ShiroUtils.generateSalt(3);
        tbUser.setSalt(salt);
        String md5 = ShiroUtils.encryptPassword("MD5", tbUser.getPassword(), salt, 3);
        tbUser.setPassword(md5);
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        tbUser.setStatus("Y");
        tbUserMapper.insertSelective(tbUser);
    }

    @Override
    public TbUser findUserByUsername(String username) {
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<TbUser> tbUsers = tbUserMapper.selectByExample(example);
        return (tbUsers == null || tbUsers.isEmpty()) ? null : tbUsers.get(0);
    }
}
